﻿using System;
using System.Linq;
using GatewayBizProcess.Db;
using Hjt212Parser;
using log4net;

namespace GatewayBizProcess.Facade
{
    public class Upline
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(Upline));
        public void ProcessBizMsg(object state)
        {
            while (true)
            {
                try
                {
                    using (var ctx = new GatewayTotalPilotContext())
                    {
                        var records = ctx.received_msg.Where(t => t.is_length_right)
                            .Where(t => t.processed_flag.ToLower().Equals(ProcessFlag.NotProcessed.ToString().ToLower()))
                            .OrderBy(t => t.id)
                            .Take(200)
                            .ToList();
                        foreach (var record in records)
                        {
                            Log.Info( record.received_string);
                            //todo:biz
                            switch (record.cn)
                            {
                                case "3071":
                                    CommandUp.Up3071(record.received_string);
                                    break;
                                case "3081":
                                    CommandUp.Up3081(record.received_string);
                                    break;
                                case "3082":
                                    CommandUp.Up3082(record.received_string);
                                    break;
                                case "9011":
                                    CommandUp.Up9011(record.received_string);
                                    break;
                                case "9012":
                                    CommandUp.Up9012(record.received_string);
                                    break;
                                case "9014":
                                    CommandUp.Up9014(record.received_string);
                                    break;
                                case "3052":
                                    CommandUp.Up3052(record.received_string);
                                    break;
                                case "3097":
                                    CommandUp.Up3097(record.received_string);
                                    break;
                                case "4012":
                                    CommandUp.Up4012(record.received_string);
                                    break;
                                case "9999":
                                    SetOnline(record.received_string);
                                    break;
                                case "0000":
                                    SetOffline(record.received_string);
                                    break;
                            }
                            record.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                        }
                        ctx.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                finally
                {
                    ThreadUtil.Sleep(1000);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
        /// <summary>
        /// 在线
        /// </summary>
        private void SetOnline(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                using (var pCtx = new PlatformZrContext())
                {
                    var statuses = pCtx.state_realtime.Where(t => t.mn.Equals(mn));
                    foreach (var stateRealtime in statuses)
                    {
                        stateRealtime.is_online = true;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error( ex);
            }
        }
        private void SetOffline(string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                using (var pCtx = new PlatformZrContext())
                {
                    var statuses = pCtx.state_realtime.Where(t => t.mn.Equals(mn));
                    foreach (var stateRealtime in statuses)
                    {
                        stateRealtime.is_online = false;
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        // ReSharper disable once FunctionNeverReturns
    }
}

